﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Web.Security;
using System.Data;

public partial class Doma : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (isUserLoggedIn())
        {
            btnOdlogiranje.Visible = true;
            lblKorisnik.Text = "Најавен корисник: " +(string)Session["korisnik"];
            Najnovi_modeli();
            SqlConnection konekcija = new SqlConnection();
            konekcija.ConnectionString = ConfigurationManager.ConnectionStrings["mojaKonekcija"].ConnectionString;
            string sqlString = "SELECT ime FROM Info";
            SqlCommand komanda = new SqlCommand(sqlString, konekcija);
            try
            {
                konekcija.Open();
                SqlDataReader citac = komanda.ExecuteReader();
                while (citac.Read())
                {
                    lblNaslov.Text = citac["ime"].ToString();
                }
                citac.Close();
            }
            catch (Exception err)
            {
                lblNaslov.Text = err.Message;
            }
            finally
            {
                konekcija.Close();
            }
            return;

        }
        else
        {
            btnLogiranje.Visible = true;
        }
        if (!IsPostBack)
        {
            SqlConnection konekcija = new SqlConnection();
            konekcija.ConnectionString = ConfigurationManager.ConnectionStrings["mojaKonekcija"].ConnectionString;
            string sqlString = "SELECT ime FROM Info";
            SqlCommand komanda = new SqlCommand(sqlString, konekcija);
            try
            {
                konekcija.Open();
                SqlDataReader citac = komanda.ExecuteReader();
                while (citac.Read())
                {
                    lblNaslov.Text = citac["ime"].ToString();
                }
                citac.Close();
            }
            catch (Exception err)
            {
                lblNaslov.Text = err.Message;
            }
            finally
            {
                konekcija.Close();
            }
        }
        Najnovi_modeli();
    }
    private bool isUserLoggedIn()
    {
        if (HttpContext.Current.User != null)
        {
            if (HttpContext.Current.User.Identity.IsAuthenticated)
            {
                return true;
            }
        }
        return false;
    }
    protected void Menu2_MenuItemClick(object sender, MenuEventArgs e)
    {
        if (Menu2.SelectedValue == "Маици")
            Response.Redirect("za_mazi.aspx?tip=maici");
        if (Menu2.SelectedValue == "Патики")
            Response.Redirect("za_mazi.aspx?tip=patiki");
        if (Menu2.SelectedValue == "Панталони")
            Response.Redirect("za_mazi.aspx?tip=pantaloni");
        if (Menu2.SelectedValue == "Фустани")
            Response.Redirect("za_zeni.aspx?tip=fustani");
        if (Menu2.SelectedValue == "Штикли")
            Response.Redirect("za_zeni.aspx?tip=stikli");
        if (Menu2.SelectedValue == "Капи")
            Response.Redirect("za_zeni.aspx?tip=kapi");
        if (Menu2.SelectedValue == "Облека за мажи")
            Response.Redirect("za_mazi.aspx?tip=mazi");
        if (Menu2.SelectedValue == "Облека за жени")
            Response.Redirect("za_zeni.aspx?tip=zeni");
    }
    protected void btnLogiranje_Click(object sender, EventArgs e)
    {
        Response.Redirect("Login.aspx");
    }
    protected void btnOdlogiranje_Click(object sender, EventArgs e)
    {

        FormsAuthentication.SignOut();

        // kolaceto istekuva vcera, t.e. ne e validno
        if (FormsAuthentication.FormsCookieName != null)
        {
            Context.Response.Cookies[FormsAuthentication.FormsCookieName].Expires = DateTime.Now.AddDays(-1);
        }

        // prefrli na default stranata
        Response.Redirect("Doma.aspx");
        
    }
    protected void Najnovi_modeli()
    {
        int max = 0;
        max = gvMaici.Rows.Count;
        for (int i = 0; i < gvMaici.Rows.Count; i++)
        {
            if (gvMaici.Rows[i].Cells[0].Text == Convert.ToString(max))
            {
                imgMaica.ImageUrl = gvMaici.Rows[i].Cells[3].Text;
                lblMaica.Text = gvMaici.Rows[i].Cells[2].Text;
            }
        }
        int max1=0;
        max1=gvPatiki.Rows.Count;
        for(int i=0; i< gvPatiki.Rows.Count;i++)
        {
            if(gvPatiki.Rows[i].Cells[0].Text==Convert.ToString(max1))
            {
                imgPatiki.ImageUrl=gvPatiki.Rows[i].Cells[3].Text;
                lblPatiki.Text=gvPatiki.Rows[i].Cells[2].Text;
            }
        }
        int max2 = 0;
        max2 = gvPantaloni.Rows.Count;
        for (int i = 0; i < gvPantaloni.Rows.Count; i++)
        {
            if (gvPantaloni.Rows[i].Cells[0].Text == Convert.ToString(max2))
            {
                imgPantaloni.ImageUrl = gvPantaloni.Rows[i].Cells[3].Text;
                lblPantaloni.Text = gvPantaloni.Rows[i].Cells[2].Text;
            }
        }
        int max3 = 0;
        max3 = gvFustan.Rows.Count;
        for (int i = 0; i < gvFustan.Rows.Count; i++)
        {
            if (gvFustan.Rows[i].Cells[0].Text == Convert.ToString(max3))
            {
                imgFustan.ImageUrl = gvFustan.Rows[i].Cells[3].Text;
                lblFustan.Text = gvFustan.Rows[i].Cells[2].Text;
            }
        }
        int max4 = 0;
        max4 = gvStikli.Rows.Count;
        for (int i = 0; i < gvStikli.Rows.Count; i++)
        {
            if (gvStikli.Rows[i].Cells[0].Text == Convert.ToString(max4))
            {
                imgStikli.ImageUrl = gvStikli.Rows[i].Cells[3].Text;
                lblStikli.Text = gvStikli.Rows[i].Cells[2].Text;
            }
        }
        int max5 = 0;
        max5= gvKapi.Rows.Count;
        for (int i = 0; i < gvKapi.Rows.Count; i++)
        {
            if (gvKapi.Rows[i].Cells[0].Text == Convert.ToString(max5))
            {
                imgKapi.ImageUrl = gvKapi.Rows[i].Cells[3].Text;
                lblKapa.Text =gvKapi.Rows[i].Cells[2].Text;
            }
        }
    }
    
}